﻿using BouncingBallGame.ViewModels;
using BouncingBallGame.ViewModels.ShapeViewModels;
using Dpy.MathWinRT.Geometry.Geometry2D;
using System;
using System.Linq;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace BouncingBallGame.Views
{
    public sealed partial class PlayGroundView : UserControl
    {
        private PlayGroundVM dataContext = null;
        private Vector2D startCoordinate = null;
        private DateTime startTime;

        public Vector2D StartCoordinate
        {
            get
            {
                return this.startCoordinate;
            }
            set
            {
                this.startCoordinate = value;
                this.startTime = DateTime.Now;
            }
        }

        public PlayGroundVM PlayGroundVM
        {
            get
            {
                if (this.dataContext == null)
                {
                    this.dataContext = this.DataContext as PlayGroundVM;
                }

                return this.dataContext;
            }
        }

        public PlayGroundView()
        {
            this.InitializeComponent();
        }

        void Ball_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
        {
            if (this.StartCoordinate == null)
            {
                return;
            }

            TimeSpan timeDifference = DateTime.Now - this.startTime;
            Vector2D spaceDifference = this.PlayGroundVM.Ball.Center - this.StartCoordinate;
            this.PlayGroundVM.StartMovingBall(spaceDifference * (1 / timeDifference.TotalSeconds));
            this.StartCoordinate = null;
        }

        void Ball_ManipulationStarted(object sender, ManipulationStartedRoutedEventArgs e)
        {
            this.StartCoordinate = this.PlayGroundVM.Ball.Center;
            this.PlayGroundVM.OnManipulationStarted();
        }

        private void Ball_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
        {
            if (this.StartCoordinate == null)
            {
                return;
            }
            this.PlayGroundVM.StopTimer();

            Vector2D offSet = new Vector2D(e.Delta.Translation.X, e.Delta.Translation.Y);
            Vector2D endCoordinate = this.PlayGroundVM.Ball.Center + offSet;

            if (this.PlayGroundVM.CanMoveBall(this.StartCoordinate, endCoordinate))
            {
                this.PlayGroundVM.Ball.XCenter = endCoordinate.X;
                this.PlayGroundVM.Ball.YCenter = endCoordinate.Y;
            }
            else
            {
                TimeSpan timeDifference = DateTime.Now - this.startTime;
                Vector2D spaceDifference = this.PlayGroundVM.Ball.Center - this.StartCoordinate;
                this.PlayGroundVM.StartMovingBall(spaceDifference * (1 / timeDifference.TotalSeconds));
                this.StartCoordinate = null;
            }

            this.PlayGroundVM.StartTimer();
        }
    }
}
